package com.wechat.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.wechat.Util.MessageUtil;
import com.wechat.message.req.TextMessage;
import com.wechat.message.resp.Article;
import com.wechat.message.resp.Music;
import com.wechat.message.resp.MusicMessage;
import com.wechat.message.resp.NewsMessage;

/**
 * 核心服务类
 * 
 * @author chenpeng
 * @date 2017/02/24
 */
public class CoreService {
	/**
	 * 处理微信发来的请求
	 * 
	 * @param request
	 * @return xml
	 */
	public static String processRequest(HttpServletRequest request) {
		// xml格式的消息数据
		String respXml = null;
		TextMessage textMessage = new TextMessage();
		try {
			// 调用parseXml方法解析请求消息
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			// 发送方帐号
			String fromUserName = requestMap.get("FromUserName");
			// 开发者微信号
			String toUserName = requestMap.get("ToUserName");
			// 消息类型
			String msgType = requestMap.get("MsgType");
			
			//long MsgId = Long.parseLong(requestMap.get("MsgId"));
			

			// 回复文本消息
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			//textMessage.setMsgId(MsgId);

			// 文本消息
			if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
				//消息内容
				String content = requestMap.get("Content");
				if("1".equals(content)){
					textMessage.setContent("生日快乐");
					// 将文本消息对象转换成xml
					respXml = MessageUtil.messageToXml(textMessage);
				}else if("2".equals(content)){
					textMessage.setContent("XXOO");
					// 将文本消息对象转换成xml
					respXml = MessageUtil.messageToXml(textMessage);
				}else if("3".equals(content)){
					textMessage.setContent("点击进入<a href=\"http://www.baidu.com\">百度</a>");
					// 将文本消息对象转换成xml
					respXml = MessageUtil.messageToXml(textMessage);
				}else if("音乐".equals(content)){
					//创建音乐对象
					Music music = new Music();
					music.setTitle("告白气球");
					music.setDescription("周二珂");
					music.setMusicUrl("http://supercp.ngrok.cc/WeChat/music/gaobaiqiqiu.mp3");
					music.setHQMusicUrl("http://supercp.ngrok.cc/WeChat/music/gaobaiqiqiu.mp3");
					
					//创建音乐消息
					MusicMessage musicMessage = new MusicMessage();
					musicMessage.setToUserName(fromUserName);
					musicMessage.setFromUserName(toUserName);
					musicMessage.setCreateTime(new Date().getTime());
					musicMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_MUSIC);
					musicMessage.setMusic(music);
					respXml = MessageUtil.messageToXml(musicMessage);
				}else if("7".equals(content)){
					//回复单图文消息（没有图片）
					Article article = new Article();
					article.setTitle("博客");
					article.setDescription("CSDN");
					article.setPicUrl("");
					article.setUrl("http://www.baidu.com");
					
					List<Article> articleList = new ArrayList<Article>(); 
					articleList.add(article);
					
					NewsMessage newsMessage = new NewsMessage();
					newsMessage.setFromUserName(toUserName);
					newsMessage.setToUserName(fromUserName);
					newsMessage.setCreateTime(new Date().getTime());
					newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
					newsMessage.setArticleCount(articleList.size());
					newsMessage.setArticles(articleList);
					
					respXml = MessageUtil.messageToXml(newsMessage);
				}else if("8".equals(content)){
					//回复单图文消息
					Article article = new Article();
					article.setTitle("博客");
					article.setDescription("CSDN");
					article.setPicUrl("http://supercp.ngrok.cc/WeChat/image/45.jpg");
					article.setUrl("http://www.baidu.com");
					
					List<Article> articleList = new ArrayList<Article>(); 
					articleList.add(article);
					
					NewsMessage newsMessage = new NewsMessage();
					newsMessage.setFromUserName(toUserName);
					newsMessage.setToUserName(fromUserName);
					newsMessage.setCreateTime(new Date().getTime());
					newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
					newsMessage.setArticleCount(articleList.size());
					newsMessage.setArticles(articleList);
					
					respXml = MessageUtil.messageToXml(newsMessage);
				}else if("9".equals(content)){
					//回复多图文消息
					Article article = new Article();
					article.setTitle("博客");
					article.setDescription("CSDN");
					article.setPicUrl("http://supercp.ngrok.cc/WeChat/image/45.jpg");
					article.setUrl("http://www.baidu.com");
					
					Article article1 = new Article();
					article1.setTitle("博客1");
					article1.setDescription("CSDN");
					article1.setPicUrl("http://supercp.ngrok.cc/WeChat/image/45.jpg");
					article1.setUrl("http://www.baidu.com");
					
					List<Article> articleList = new ArrayList<Article>(); 
					articleList.add(article);
					articleList.add(article1);
					
					NewsMessage newsMessage = new NewsMessage();
					newsMessage.setFromUserName(toUserName);
					newsMessage.setToUserName(fromUserName);
					newsMessage.setCreateTime(new Date().getTime());
					newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
					newsMessage.setArticleCount(articleList.size());
					newsMessage.setArticles(articleList);
					
					respXml = MessageUtil.messageToXml(newsMessage);
				}
			}
			// 图片消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
				textMessage.setContent("您发送的是图片消息！");
				respXml = MessageUtil.messageToXml(textMessage);
			}
			// 语音消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
				textMessage.setContent("您发送的是语音消息！");
				respXml = MessageUtil.messageToXml(textMessage);
			}
			// 视频消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) {
				textMessage.setContent("您发送的是视频消息！");
				respXml = MessageUtil.messageToXml(textMessage);
			}
			// 地理位置消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
				textMessage.setContent("您发送的是地理位置消息！");
				respXml = MessageUtil.messageToXml(textMessage);
			}
			// 链接消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
				textMessage.setContent("您发送的是链接消息！");
				respXml = MessageUtil.messageToXml(textMessage);
			}
			// 事件推送
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				// 事件类型
				String eventType = requestMap.get("Event");
				// 关注
				if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
					textMessage.setContent("谢谢您的关注！");
					respXml = MessageUtil.messageToXml(textMessage);
				}
				// 取消关注
				else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
					// TODO 取消订阅后用户不会再收到公众账号发送的消息，因此不需要回复
				}
				// 扫描带参数二维码
				else if (eventType.equals(MessageUtil.EVENT_TYPE_SCAN)) {
					// TODO 处理扫描带参数二维码事件
				}
				 //自动上报地理位置
				else if (eventType.equals(MessageUtil.EVENT_TYPE_LOCATION)) {
					String Longitude = requestMap.get("Longitude");
					String Latitude = requestMap.get("Latitude");
					System.out.println(String.format("【自动上报地理位置】经度：%s,纬度：%s", Longitude,Latitude));
				}
				// 自定义菜单
				else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
					// TODO 处理菜单点击事件
					String eventKey = requestMap.get("EventKey");
					if(eventKey.equals("KEY_WEATHER")){
						textMessage.setContent("您点击了天气预报菜单！");
					}else if(eventKey.equals("KEY_MUSIC")){
						textMessage.setContent("您点击了每日歌曲菜单");
					}
					respXml = MessageUtil.messageToXml(textMessage);
					
				}
				//扫码带提示
				else if(eventType.equals(MessageUtil.EVENT_TYPE_SCANCODE_WAITMSG)){
					String eventKey = requestMap.get("EventKey");
					//扫码结果
					String ScanResult = requestMap.get("ScanResult");
					if(eventKey.equals("KEY_11")){
						textMessage.setContent("您点击了【扫码带提示】菜单\n\n扫码结果："+ScanResult);
					}
					respXml = MessageUtil.messageToXml(textMessage);
					
				}
				//扫码推事件
				else if(eventType.equals(MessageUtil.EVENT_TYPE_SCANCODE_PUSH)){
					String eventKey = requestMap.get("EventKey");
					//扫码结果
					String ScanResult = requestMap.get("ScanResult");
					if(eventKey.equals("KEY_12")){
						textMessage.setContent("您点击了【扫码推事件】菜单\n\n扫码结果："+ScanResult);
					}
					respXml = MessageUtil.messageToXml(textMessage);
					
				}
				//系统拍照
				else if(eventType.equals(MessageUtil.EVENT_TYPE_PIC_SYSPHOTO)){
					String eventKey = requestMap.get("EventKey");
					if(eventKey.equals("KEY_21")){
						textMessage.setContent("您点击了【系统拍照】菜单");
					}
					respXml = MessageUtil.messageToXml(textMessage);
					
				}
				//拍照或相册选择
				else if(eventType.equals(MessageUtil.EVENT_TYPE_PIC_PHOTO_OR_ALBUM)){
					String eventKey = requestMap.get("EventKey");
					if(eventKey.equals("KEY_22")){
						textMessage.setContent("您点击了【拍照或相册选择】菜单");
					}
					respXml = MessageUtil.messageToXml(textMessage);
				}
				//相册选择
				else if(eventType.equals(MessageUtil.EVENT_TYPE_PIC_WEIXIN)){
					String eventKey = requestMap.get("EventKey");
					if(eventKey.equals("KEY_23")){
						textMessage.setContent("您点击了【相册选择】菜单");
					}
					respXml = MessageUtil.messageToXml(textMessage);
				}
				//位置选择
				else if(eventType.equals(MessageUtil.EVENT_TYPE_LOCATION_SELECT)){
					String eventKey = requestMap.get("EventKey");
					String Location_X = requestMap.get("Location_X");
					String Location_Y = requestMap.get("Location_Y");
					if(eventKey.equals("KEY_31")){
						textMessage.setContent("您点击了【位置选择】菜单，\n x:"+Location_X+"y:"+Location_Y);
					}
					respXml = MessageUtil.messageToXml(textMessage);
				}
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respXml;
	}
}
